package com.eddie.jdbc.service.impl;

import com.eddie.jdbc.dao.PetDao;
import com.eddie.jdbc.dao.impl.BaseDaoImpl;
import com.eddie.jdbc.entity.PetEntity;
import com.eddie.jdbc.entity.PetTypeEntity;
import com.eddie.jdbc.entity.PetTypeVarietiesMappingEntity;
import com.eddie.jdbc.entity.PetVarietiesEntity;
import com.eddie.jdbc.service.PetService;
import com.eddie.jdbc.service.PetTypeService;
import com.eddie.jdbc.service.PetTypeVarietiesMappingService;
import com.eddie.jdbc.service.PetVarietiesService;
import com.eddie.jdbc.vo.PetVo;
import org.springframework.beans.BeanUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author Eddie
 */
public class PetServiceImpl extends BaseDaoImpl<PetEntity> implements PetService {

    private PetTypeVarietiesMappingService mappingService = new PetTypeVarietiesMappingServiceImpl();

    private PetTypeService typeService = new PetTypeServiceImpl();

    private PetVarietiesService varietiesService = new PetVarietiesServiceImpl();

    @Override
    public List<PetVo> getPetList() {
        List<PetVo> vos = new ArrayList<>();
        for (PetEntity petEntity : super.queryAll()) {
            PetVo petVo = new PetVo();
            BeanUtils.copyProperties(petEntity,petVo);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            petVo.setBirthday(sdf.format(petEntity.getBirthday()));
            PetTypeVarietiesMappingEntity mappingEntity = mappingService.getPetTypeVarietiesMapping(petEntity.getId());
            PetTypeEntity petTypeEntity = typeService.queryById(mappingEntity.getPetTypeId());
            petVo.setType(petTypeEntity.getType());
            PetVarietiesEntity petVarietiesEntity = varietiesService.queryById(mappingEntity.getPetVarietiesId());
            petVo.setVarieties(petVarietiesEntity.getVarieties());
            vos.add(petVo);
        }
        return vos;
    }
}